﻿using System;
using System.IO;
using System.Collections.Generic;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using Lottery.App.DAL;
using Lottery.App.Model;
using Lottery.App.Utility;

namespace Lottery.App.BLL
{
    public class ExportManager
    {
        public string ExportMin(string value, GameMode gm)
        {
            bool ASC_Result = true;

            IList<RecordInfo> list = null;
            List<int> red_index = new List<int>();
            string[] items = value.Split(' ');

            #region 加载数据

            switch (gm)
            {
                case GameMode.A1: list = new A1Dao().List(value); ASC_Result = false; break;
                case GameMode.A2: list = new A2Dao().List(value); ASC_Result = false; break;
                case GameMode.A3: list = new A3Dao().List(value); ASC_Result = false; break;
                case GameMode.P2: list = new P2Dao().List(value); ASC_Result = false; break;
                case GameMode.P3: list = new P3Dao().List(value); ASC_Result = false; break;
                case GameMode.N2: list = new N2Dao().List(value.Replace(" ", "%")); break;
                case GameMode.N3: list = new N3Dao().List(value.Replace(" ", "%")); break;
                case GameMode.N4: list = new N4Dao().List(value.Replace(" ", "%")); break;
                case GameMode.N5: list = new N5Dao().List(value.Replace(" ", "%")); break;
                case GameMode.D1T2:
                    list = new D1T2Dao().List(value);
                    for (int i = 0; i < list.Count; i++)
                    {
                        string tmp = list[i].Result;
                        if(tmp.Contains(items[0]) && tmp.Contains(items[1]) && tmp.Contains(items[2]))
                            red_index.Add(i);
                    }
                    break;
                case GameMode.B4Z3:
                    list = new B4Z3Dao().List(value);
                    for (int i = 0; i < list.Count; i++)
                    {
                        string tmp = list[i].Result;
                        if (tmp.Contains(items[0]) && tmp.Contains(items[1]) 
                            && tmp.Contains(items[2]) && tmp.Contains(items[3])
                            )
                            red_index.Add(i);
                    }
                    break;
                case GameMode.B5Z4:
                    list = new B5Z4Dao().List(value);
                    for (int i = 0; i < list.Count; i++)
                    {
                        string tmp = list[i].Result;
                        if (tmp.Contains(items[0]) && tmp.Contains(items[1]) 
                            && tmp.Contains(items[2]) && tmp.Contains(items[3]) 
                            && tmp.Contains(items[4]))
                            red_index.Add(i);
                    }
                    break;
                case GameMode.B6Z5:
                    list = new B6Z5Dao().List(value);
                    for (int i = 0; i < list.Count; i++)
                    {
                        string tmp = list[i].Result;
                        if (tmp.Contains(items[0]) && tmp.Contains(items[1]) 
                            && tmp.Contains(items[2]) && tmp.Contains(items[3]) 
                            && tmp.Contains(items[4]) && tmp.Contains(items[5])
                            )
                            red_index.Add(i);
                    }
                    break;
                default:
                    break;
            }
            #endregion

            // 创建工作簿
            IWorkbook workbook = new HSSFWorkbook();

            ICellStyle NormalStyle, RedStyle;

            #region 设置样式

            IFont font = null;

            // 设置默认单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = "宋体";

            // 设置默认单元格样式
            NormalStyle = workbook.CreateCellStyle();
            NormalStyle.SetFont(font);
            NormalStyle.Alignment = HorizontalAlignment.CENTER;
            NormalStyle.VerticalAlignment = VerticalAlignment.CENTER;
            NormalStyle.WrapText = true;

            // 设置全中单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontName = "宋体";
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            // 设置全中单元格样式
            RedStyle = workbook.CreateCellStyle();
            RedStyle.SetFont(font);
            RedStyle.Alignment = HorizontalAlignment.CENTER;
            RedStyle.VerticalAlignment = VerticalAlignment.CENTER;
            RedStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index;
            RedStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

            #endregion

            // 创建工作表
            ISheet sheet = workbook.CreateSheet(String.Format("投注方案 {0}", value));

            // 设置列宽
            sheet.SetColumnWidth(0, 20 * 256);
            sheet.SetColumnWidth(1, 35 * 256);
            sheet.SetColumnWidth(2, 10 * 256);

            // 设置默认样式
            sheet.SetDefaultColumnStyle(0, NormalStyle);
            sheet.SetDefaultColumnStyle(1, NormalStyle);
            sheet.SetDefaultColumnStyle(2, NormalStyle);


            // 创建表头
            IRow row = sheet.CreateRow(0);
            row.HeightInPoints = 25;
            row.CreateCell(0).SetCellValue("期号");
            row.CreateCell(1).SetCellValue("开奖号码");
            row.CreateCell(2).SetCellValue(value);

            #region 开始写入数据

            // 创建第一行数据
            row = sheet.CreateRow(1);
            row.HeightInPoints = 25;
            row.CreateCell(0).SetCellValue(list[0].IssueNum);
            row.CreateCell(1).SetCellValue(ASC_Result ? list[0].ASC_Result : list[0].Result);
            row.CreateCell(2).SetCellValue(list[0].ID - 1);  // 自身是开奖期,所以要减一.

            // 创建第二行及以后的数据
            if (ASC_Result)
            {
                for (int i = 1; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i + 1);
                    row.HeightInPoints = 25;

                    row.CreateCell(0).SetCellValue(list[i].IssueNum);
                    row.CreateCell(1).SetCellValue(list[i].ASC_Result);
                    row.CreateCell(2).SetCellValue(list[i].ID - list[i - 1].ID - 1); // 两期之间的遗漏,所以要减一.
                }
            }
            else
            {
                for (int i = 1; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i + 1);
                    row.HeightInPoints = 25;

                    row.CreateCell(0).SetCellValue(list[i].IssueNum);
                    row.CreateCell(1).SetCellValue(list[i].Result);
                    row.CreateCell(2).SetCellValue(list[i].ID - list[i - 1].ID - 1); // 两期之间的遗漏,所以要减一.
                }
            }
            #endregion

            // 标记全中行
            foreach (int index in red_index)
            {
                sheet.GetRow(index + 1).GetCell(2).CellStyle = RedStyle;
            }

            //冻结首行(标题行)
            sheet.CreateFreezePane(0, 1, 0, 1);

            string path = String.Format("data\\{0}.xls", Guid.NewGuid());
            
            FileStream fs = File.Create(path);
            workbook.Write(fs);
            fs.Close();

            return path;
        }

        public string Export(string value, GameMode gm)
        {
            bool ASC_Result = true;
            RecordDao dao = new RecordDao();
            IList<RecordInfo> list = GlobalData.Records; //dao.GetAllObjects();
            IList<int> ids_green = null;
            IList<int> ids_red = new List<int>();

            #region 加载数据

            switch (gm)
            {
                case GameMode.A1: ids_green = new A1Dao().GetIds(value); ASC_Result = false; break;
                case GameMode.A2: ids_green = new A2Dao().GetIds(value); ASC_Result = false; break;
                case GameMode.A3: ids_green = new A3Dao().GetIds(value); ASC_Result = false; break;
                case GameMode.P2: ids_green = new P2Dao().GetIds(value); ASC_Result = false; break;
                case GameMode.P3: ids_green = new P3Dao().GetIds(value); ASC_Result = false; break;
                case GameMode.N2: ids_green = new N2Dao().GetIds(value); break;
                case GameMode.N3: ids_green = new N3Dao().GetIds(value); break;
                case GameMode.N4: ids_green = new N4Dao().GetIds(value); break;
                case GameMode.N5: ids_green = new N5Dao().GetIds(value); break;
                case GameMode.D1T2:
                    ids_green = new D1T2Dao().GetIds(value);
                    value = Utils.Sort(value);
                    ids_red = new N3Dao().GetIds(value);
                    break;
                case GameMode.B4Z3:
                    ids_green = new B4Z3Dao().GetIds(value);
                    ids_red = new N4Dao().GetIds(value);
                    break;
                case GameMode.B5Z4:
                    ids_green = new B5Z4Dao().GetIds(value);
                    ids_red = new N5Dao().GetIds(value);
                    break;
                case GameMode.B6Z5:
                    ids_green = new B6Z5Dao().GetIds(value);
                    ids_red = new N5Dao().GetIds(value); //这里是继承父类的方法。所以虽然是六个号码，但不影响结果。
                    break;
                default:
                    throw new ArgumentOutOfRangeException("");
            }
            
            #endregion

            // 创建工作簿
            IWorkbook workbook = new HSSFWorkbook();

            ICellStyle NormalStyle, TitleStyle, FormulaStyle, GreenStyle, RedStyle;

            #region 设置样式

            IFont font = null;

            // 设置默认单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = "宋体";

            // 设置默认单元格样式
            NormalStyle = workbook.CreateCellStyle();
            NormalStyle.SetFont(font);
            NormalStyle.Alignment = HorizontalAlignment.CENTER;
            NormalStyle.VerticalAlignment = VerticalAlignment.CENTER;

            // 设置标题单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = "黑体";

            // 设置标题单元格样式
            TitleStyle = workbook.CreateCellStyle();
            TitleStyle.SetFont(font);
            TitleStyle.Alignment = HorizontalAlignment.CENTER;
            TitleStyle.VerticalAlignment = VerticalAlignment.CENTER;
            TitleStyle.WrapText = true;


            // 设置公式列单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontName = "宋体";
            font.Color = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;

            // 设置公式列单元格样式
            FormulaStyle = workbook.CreateCellStyle();
            FormulaStyle.SetFont(font);
            FormulaStyle.Alignment = HorizontalAlignment.CENTER;
            FormulaStyle.VerticalAlignment = VerticalAlignment.CENTER;

            // 设置目标单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontName = "宋体";
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            // 设置目标单元格样式
            GreenStyle = workbook.CreateCellStyle();
            GreenStyle.SetFont(font);
            GreenStyle.Alignment = HorizontalAlignment.CENTER;
            GreenStyle.VerticalAlignment = VerticalAlignment.CENTER;
            GreenStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREEN.index;
            GreenStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

            // 设置全中单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontName = "宋体";
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            // 设置全中单元格样式
            RedStyle = workbook.CreateCellStyle();
            RedStyle.SetFont(font);
            RedStyle.Alignment = HorizontalAlignment.CENTER;
            RedStyle.VerticalAlignment = VerticalAlignment.CENTER;
            RedStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index;
            RedStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

            #endregion

            // 创建工作表
            ISheet sheet = workbook.CreateSheet(String.Format("投注方案 {0}", value));

            // 设置列宽
            sheet.SetColumnWidth(0, 20 * 256);
            sheet.SetColumnWidth(1, 35 * 256);
            sheet.SetColumnWidth(2, 10 * 256);

            // 设置默认样式
            sheet.SetDefaultColumnStyle(0, NormalStyle);
            sheet.SetDefaultColumnStyle(1, NormalStyle);
            sheet.SetDefaultColumnStyle(2, FormulaStyle);


            // 创建表头
            IRow row = sheet.CreateRow(0);
            row.HeightInPoints = 30;
            row.CreateCell(0).SetCellValue("期号");
            row.CreateCell(1).SetCellValue("开奖号码");
            row.CreateCell(2).SetCellValue(value);
            row.GetCell(0).CellStyle = TitleStyle;
            row.GetCell(1).CellStyle = TitleStyle;
            row.GetCell(2).CellStyle = TitleStyle;


            // 创建第一行数据
            row = sheet.CreateRow(1);
            row.HeightInPoints = 25;
            row.CreateCell(0).SetCellValue(list[0].IssueNum);
            row.CreateCell(1).SetCellValue(ASC_Result ? list[0].ASC_Result : list[0].Result);
            row.CreateCell(2).SetCellValue(1);

            if (ASC_Result)
            {
                for (int i = 1; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i + 1);
                    row.HeightInPoints = 25;

                    row.CreateCell(0).SetCellValue(list[i].IssueNum);
                    row.CreateCell(1).SetCellValue(list[i].ASC_Result);
                    row.CreateCell(2).CellFormula = String.Format("IF(C{0}>0,C{0}+1,1)", i + 1);
                }
            }
            else
            {
                for (int i = 1; i < list.Count; i++)
                {
                    row = sheet.CreateRow(i + 1);
                    row.HeightInPoints = 25;

                    row.CreateCell(0).SetCellValue(list[i].IssueNum);
                    row.CreateCell(1).SetCellValue(list[i].Result);
                    row.CreateCell(2).CellFormula = String.Format("IF(C{0}>0,C{0}+1,1)", i + 1);
                }
            }
            
            // 标记中奖期数
            ICell cell = null;
            int count = -1;
            foreach (int index in ids_green)
            {
                cell = sheet.GetRow(index).GetCell(2);
                cell.CellFormula = String.Empty;
                cell.SetCellValue(count--);
                cell.CellStyle = GreenStyle;
            }

            // 标记全中期数
            foreach (int index in ids_red)
            {
                sheet.GetRow(index).GetCell(2).CellStyle = RedStyle;
            }

            //冻结首行(标题行)
            sheet.CreateFreezePane(0, 1, 0, 1);

            string path = String.Format("data\\{0}.xls", Guid.NewGuid());

            FileStream fs = File.Create(path);
            workbook.Write(fs);
            fs.Close();

            return path;
        }

        public string ExportLimit(List<string> values, GameMode gm)
        {
            const int limit = 840;

            RecordDao dao = new RecordDao();

            IList<RecordInfo> list = new List<RecordInfo>(limit);
            for (int i = GlobalData.Records.Count - limit; i < GlobalData.Records.Count; i++)
                list.Add(GlobalData.Records[i]);
            
            IList<int> ids_green = null;
            IList<int> ids_red = new List<int>();

            // 创建工作簿
            IWorkbook workbook = new HSSFWorkbook();

            ICellStyle NormalStyle, TitleStyle, FormulaStyle, GreenStyle, RedStyle;

            #region 设置样式

            IFont font = null;

            // 设置默认单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = "宋体";

            // 设置默认单元格样式
            NormalStyle = workbook.CreateCellStyle();
            NormalStyle.SetFont(font);
            NormalStyle.Alignment = HorizontalAlignment.CENTER;
            NormalStyle.VerticalAlignment = VerticalAlignment.CENTER;

            // 设置标题单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = "黑体";

            // 设置标题单元格样式
            TitleStyle = workbook.CreateCellStyle();
            TitleStyle.SetFont(font);
            TitleStyle.Alignment = HorizontalAlignment.CENTER;
            TitleStyle.VerticalAlignment = VerticalAlignment.CENTER;
            TitleStyle.WrapText = true;


            // 设置公式列单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontName = "宋体";
            font.Color = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;

            // 设置公式列单元格样式
            FormulaStyle = workbook.CreateCellStyle();
            FormulaStyle.SetFont(font);
            FormulaStyle.Alignment = HorizontalAlignment.CENTER;
            FormulaStyle.VerticalAlignment = VerticalAlignment.CENTER;

            // 设置目标单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontName = "宋体";
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            // 设置目标单元格样式
            GreenStyle = workbook.CreateCellStyle();
            GreenStyle.SetFont(font);
            GreenStyle.Alignment = HorizontalAlignment.CENTER;
            GreenStyle.VerticalAlignment = VerticalAlignment.CENTER;
            GreenStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREEN.index;
            GreenStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

            // 设置全中单元格字体
            font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontName = "宋体";
            font.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

            // 设置全中单元格样式
            RedStyle = workbook.CreateCellStyle();
            RedStyle.SetFont(font);
            RedStyle.Alignment = HorizontalAlignment.CENTER;
            RedStyle.VerticalAlignment = VerticalAlignment.CENTER;
            RedStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index;
            RedStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

            #endregion

            // 创建工作表
            ISheet sheet = workbook.CreateSheet("Sheet1");

            // 设置列宽
            sheet.SetColumnWidth(0, 20 * 256);
            sheet.SetColumnWidth(1, 35 * 256);
            for (int i = 0; i < values.Count; i++)
                sheet.SetColumnWidth(2 + i, 10 * 256);
            

            // 设置默认样式
            sheet.SetDefaultColumnStyle(0, NormalStyle);
            sheet.SetDefaultColumnStyle(1, NormalStyle);
            for (int i = 0; i < values.Count; i++)
                sheet.SetDefaultColumnStyle(2 + i, FormulaStyle);


            // 创建表头
            IRow row = sheet.CreateRow(0);
            row.HeightInPoints = 30;
            row.CreateCell(0).SetCellValue("期号");
            row.CreateCell(1).SetCellValue("开奖号码");
            for (int i = 0; i < values.Count; i++)
                row.CreateCell(2 + i).SetCellValue(values[i]);
            row.GetCell(0).CellStyle = TitleStyle;
            row.GetCell(1).CellStyle = TitleStyle;
            for (int i = 0; i < values.Count; i++)
                row.GetCell(2 + i).CellStyle = TitleStyle;


            // 创建第一行数据
            row = sheet.CreateRow(1);
            row.HeightInPoints = 25;
            row.CreateCell(0).SetCellValue(list[0].IssueNum);
            row.CreateCell(1).SetCellValue(list[0].ASC_Result);
            for (int i = 0; i < values.Count; i++)
                row.CreateCell(2 + i).SetCellValue(1);

            for (int i = 1; i < list.Count; i++)
            {
                row = sheet.CreateRow(i + 1);
                row.HeightInPoints = 25;

                row.CreateCell(0).SetCellValue(list[i].IssueNum);
                row.CreateCell(1).SetCellValue(list[i].ASC_Result);
                for (int j = 0; j < values.Count; j++)
                    row.CreateCell(2 + j).CellFormula = String.Format("IF({1}{0}>0,{1}{0}+1,1)", i + 1, Utils.ConvertColumnIndexToColumnName(j + 2));
            }

            
            // 标记中奖期数
            int start = GlobalData.Records.Count - limit;
            ICell cell = null;
            for (int offset = 0; offset < values.Count; offset++)
            {
                string value = values[offset];

                #region 加载数据

                switch (gm)
                {
                    //case GameMode.A1: ids_green = new A1Dao().GetIds(value, start); break;
                    //case GameMode.A2: ids_green = new A2Dao().GetIds(value, start); break;
                    //case GameMode.A3: ids_green = new A3Dao().GetIds(value, start); break;
                    //case GameMode.P2: ids_green = new P2Dao().GetIds(value, start); break;
                    //case GameMode.P3: ids_green = new P3Dao().GetIds(value, start); break;
                    //case GameMode.B4P3: ids_green = new B4P3Dao().GetIds(value, start); break;
                    //case GameMode.B7P3: ids_green = new B7P3Dao().GetIds(value, start); break;
                    case GameMode.N2: ids_green = new N2Dao().GetIds(value, start); break;
                    case GameMode.N3: ids_green = new N3Dao().GetIds(value, start); break;
                    case GameMode.N4: ids_green = new N4Dao().GetIds(value, start); break;
                    case GameMode.N5: ids_green = new N5Dao().GetIds(value, start); break;
                    case GameMode.D1T2:
                        ids_green = new D1T2Dao().GetIds(value, start);
                        value = Utils.Sort(value);
                        ids_red = new N3Dao().GetIds(value, start);
                        break;
                    case GameMode.B4Z3:
                        ids_green = new B4Z3Dao().GetIds(value, start);
                        ids_red = new N4Dao().GetIds(value, start);
                        break;
                    case GameMode.B5Z4:
                        ids_green = new B5Z4Dao().GetIds(value, start);
                        ids_red = new N5Dao().GetIds(value, start);
                        break;
                    case GameMode.B6Z5:
                        ids_green = new B6Z5Dao().GetIds(value, start);
                        ids_red = new N5Dao().GetIds(value, start); //这里是继承父类的方法。所以虽然是六个号码，但不影响结果。
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                #endregion

                int count = -1;
                foreach (int index in ids_green)
                {
                    cell = sheet.GetRow(index - start).GetCell(2 + offset);
                    cell.CellFormula = String.Empty;
                    cell.SetCellValue(count--);
                    cell.CellStyle = GreenStyle;
                }

                // 标记全中期数
                foreach (int index in ids_red)
                {
                    sheet.GetRow(index - start).GetCell(2 + offset).CellStyle = RedStyle;
                }
            }
            
            //冻结首行(标题行)
            sheet.CreateFreezePane(2, 1, 2, 1);

            string path = String.Format("data\\{0}.xls", Guid.NewGuid());

            FileStream fs = File.Create(path);
            workbook.Write(fs);
            fs.Close();

            return path;
        }
    }
}
